﻿using System;
using System.Drawing.Text;
using System.Windows.Forms;
using AnalogInput;
using EONFULLSIN.Utils;
using EONFULLSIN.Views.Device;

namespace EONFULLSIN
{
    internal static class Program
    {
        private static MainWindow mainWindow;
        public static AdamHelper adamA;
        public static AdamHelper adamB;
    

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        private static void Main()
        {
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(
                Application_ThreadException
            );
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(
                CurrentDomain_UnhandledException
            );
            AntdUI.Localization.DefaultLanguage = "zh-CN";
            LanguageHelper.CurrentLanguage = AppSetting.GetAppSettings()["Language"]?.ToString();
            AntdUI.Config.TextRenderingHint = System
                .Drawing
                .Text
                .TextRenderingHint
                .AntiAliasGridFit;
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            mainWindow = new MainWindow();
            //启动硬件
            //ModbusTcpHelperZlan zlanModbus = new ModbusTcpHelperZlan("dynamometer1", "192.168.1.200",8801);
            //ModbusTcpHelperZlan jyModbus = new ModbusTcpHelperZlan("jy","192.168.1.232",10000);
            adamA = new AdamHelper("COM1", AdamTaskQueueHelper.SharedQueueIns_A);
            adamB = new AdamHelper("COM2", AdamTaskQueueHelper.SharedQueueIns_B);
            //zlanModbus.Ready(4096, 22, 1);
            //jyModbus.Ready(0, 32, 254);
            adamA.Ready();
            adamB.Ready();
         

            Application.Run(mainWindow);
        }

        // 捕获UI线程中的未处理异常
        private static void Application_ThreadException(
            object sender,
            System.Threading.ThreadExceptionEventArgs e
        )
        {
            LoggerHelper.Logger.Fatal(e.Exception, "未处理的UI线程异常");
            AntdUI.Notification.error(
                mainWindow,
                "未处理的UI线程异常",
                e.Exception.Message,
                autoClose: 3,
                align: AntdUI.TAlignFrom.TR
            );
        }

        // 捕获非UI线程中的未处理异常
        private static void CurrentDomain_UnhandledException(
            object sender,
            UnhandledExceptionEventArgs e
        )
        {
            LoggerHelper.Logger.Fatal(e.ExceptionObject as Exception, "未处理的非UI线程异常");
            AntdUI.Notification.error(
                mainWindow,
                "未处理的非UI线程异常",
                e.ToString(),
                autoClose: 3,
                align: AntdUI.TAlignFrom.TR
            );
        }
    }
}
